/**
 * 
 */
package com.stock.calculator;

import java.io.IOException;
import java.util.Scanner;

import com.stock.calculator.helper.FederalNoteHelper;
import com.stock.calculator.helper.StockHelper;

/**
 * @author Ankit Dimania
 * 
 */
public class Main {

    /**
     * @param args
     *            Command Line Arguments
     * @throws IOException
     */
    private static final String tickerSymbol = "FLXS";

    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws IOException {
        StockHelper stockHelper = new StockHelper(tickerSymbol);

        String[][] tenYrSummary = stockHelper.fetch10YrSummaryTable();
        String[][] financialResult = stockHelper.fetchFinancialResultTable();
        String[] tenYrBookValueTable = stockHelper.fetch10YrBookValueTable();

        String[][] Table = FederalNoteHelper.fetchDailyYieldCurveRateTable();
        print("Federal Note Rate", Table);

        double tenYrFederalNoteRate = Double.parseDouble(FederalNoteHelper.fetchLatest10YrYieldCurveRate());

        System.out.println("Current 10 Year Yield Curve Rate : " + tenYrFederalNoteRate);
        System.out.println();
        print("Ten Year Summary", tenYrSummary);
        System.out.println();
        print("Financial Headdings", financialResult);
        System.out.println();

        Double[] bookValueGrowthRate = StockCalculator.calcGrowthRate(tenYrBookValueTable);

        print("Book Value Growth Rate : ", tenYrBookValueTable, bookValueGrowthRate);

        double dividend = 0;
        try {
            dividend = Double.parseDouble(stockHelper.fetchDividendYield());
        } catch (NumberFormatException ex) {
        }
        double currentBookValue = Double.parseDouble(stockHelper.fetchCurrentBookValue());
        double earningsPerShare = Double.parseDouble(stockHelper.fetchEarningsPerShare());

        double intrinsicEarningPrice = StockCalculator.calcEarningPrice(earningsPerShare);
        double intrinsicEarningBargainPrice = StockCalculator.calcBargainValue(intrinsicEarningPrice);
        double intrinsicBookValuePrice = StockCalculator.calcBookValuePrice(currentBookValue);
        double intrinsicBookValueBargainPrice = StockCalculator.calcBargainValue(intrinsicBookValuePrice);
        double intrinsicEarningAndBookValuePrice = StockCalculator.calcEarningAndBookValuePrice(earningsPerShare,
                currentBookValue);
        double intrinsicEarningAndBookValueBargainPrice = StockCalculator
                .calcBargainValue(intrinsicEarningAndBookValuePrice);
        System.out.format("Max value of Stock based on EPS \t\t\t : %.2f \t Bargain Price : %.2f \n",
                intrinsicEarningPrice, intrinsicEarningBargainPrice);
        System.out.format("Max value of Stock based on BookValue \t\t\t : %.2f \t Bargain Price : %.2f \n",
                intrinsicBookValuePrice, intrinsicBookValueBargainPrice);
        System.out.format("Max value of Stock based on EPS and BookValue \t\t : %.2f \t Bargain Price : %.2f \n",
                intrinsicEarningAndBookValuePrice, intrinsicEarningAndBookValueBargainPrice);

        double growthRate = bookValueGrowthRate[0];
        printIntrinsicValue(dividend, currentBookValue, growthRate, tenYrFederalNoteRate);

        String fiveYrEarningsGrowthEstimate = stockHelper.fetch5YrEarningsGrowthRate().replaceAll("[^0-9.]", "");

        if (!fiveYrEarningsGrowthEstimate.isEmpty()) {
            growthRate = Double.parseDouble(fiveYrEarningsGrowthEstimate);
            // Dividend is not considered while calculating future growth, I
            // believe.
            printIntrinsicValue(0, currentBookValue, growthRate, tenYrFederalNoteRate);
        }

        while (true) {
            Scanner scanner = new Scanner(System.in);
            growthRate = scanner.nextDouble();

            printIntrinsicValue(dividend, currentBookValue, growthRate, tenYrFederalNoteRate);
        }
    }

    public static void printIntrinsicValue(double dividend, double currentBookValue, double growthRate,
            double tenYrFederalNoteRate) {
        double intrinsicValue = StockCalculator.calcIntrinsicValue(dividend, currentBookValue, growthRate, 10,
                tenYrFederalNoteRate);
        double bargainPrice = StockCalculator.calcBargainValue(intrinsicValue);

        System.out.format("Intrinsic Value of Stock based on Growth %.2f%%  \t : %.2f \t Bargain Price : %.2f \n",
                growthRate, intrinsicValue, bargainPrice);
    }

    public static void print(String headding, String[][] result) {
        if (result != null) {
            System.out.println(headding);
            for (String[] rows : result) {
                for (String string : rows)
                    System.out.format("%23s", string);
                System.out.println();
            }
        }
        System.out.println();
    }

    public static <T> void print(String headding, T[]... result) {
        if (result != null) {
            System.out.println(headding);

            if (result.length > 0)
                for (int i = 0; i < result[0].length; i++) {
                    for (int j = 0; j < result.length; j++) {
                        if (Number.class.isAssignableFrom(result[j][i].getClass()))
                            System.out.format("%15.2f", ((Number) result[j][i]).doubleValue());
                        else
                            System.out.format("%15s", result[j][i]);
                    }
                    System.out.println();
                }
        }
        System.out.println();
    }

}
